package com.tc.controller;

import com.tc.dto.Insured_personDTO;
import com.tc.dto.Insured_personQueryDTO;
import com.tc.service.IInsured_personService;
import com.tc.utils.ResultVo;
import com.tc.vo.Hospital_reimbursement_ratioVO;
import com.tc.vo.PageResultVO;
import com.tc.vo.PatientReimbursementRatioVO;
import com.tc.vo.Insured_personVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  被保险人前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/insured_person")
@Api(tags = "被保险人（患者）管理")
public class Insured_personController {

    @Autowired
    private IInsured_personService insuredPersonService;

    @ApiOperation(value = "分页查询被保险人（患者）")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Insured_personVO>> pageQuery(@RequestBody Insured_personQueryDTO queryDTO) {
        PageResultVO<Insured_personVO> pageResultVO = insuredPersonService.pageQuery(queryDTO);
        return ResultVo.ok(pageResultVO);
    }

    @ApiOperation(value = "根据ID查询被保险人(患者)")
    @ApiImplicitParam(name = "id", value = "被保险人ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/{id}")
    public ResultVo<Insured_personVO> getById(@PathVariable Long id) {
        Insured_personVO vo = insuredPersonService.getInsuredPersonById(id);
        if (vo != null) {
            return ResultVo.ok(vo);
        } else {
            return ResultVo.fail("被保险人不存在");
        }
    }

    @ApiOperation(value = "新增被保险人(患者)")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody @Valid Insured_personDTO insuredPersonDTO) {
        boolean success = insuredPersonService.addInsuredPerson(insuredPersonDTO);
        if (success) {
            return ResultVo.ok(true, "新增成功");
        } else {
            return ResultVo.fail("新增失败");
        }
    }

    @ApiOperation(value = "修改被保险人(患者)")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody @Valid Insured_personDTO insuredPersonDTO) {
        if (insuredPersonDTO.getPatientId() == null) {
            return ResultVo.fail("ID不能为空");
        }
        
        boolean success = insuredPersonService.updateInsuredPerson(insuredPersonDTO);
        if (success) {
            return ResultVo.ok(true, "修改成功");
        } else {
            return ResultVo.fail("修改失败");
        }
    }

    @ApiOperation(value = "删除被保险人(患者)")
    @ApiImplicitParam(name = "id", value = "被保险人ID", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultVo<Boolean> delete(@PathVariable Long id) {
        boolean success = insuredPersonService.deleteInsuredPerson(id);
        if (success) {
            return ResultVo.ok(true, "删除成功");
        } else {
            return ResultVo.fail("删除失败");
        }
    }
    
    @ApiOperation(value = "根据患者ID查询医保报销比例")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/reimbursement/{patientId}")
    public ResultVo<List<PatientReimbursementRatioVO>> getReimbursementRatio(@PathVariable Long patientId) {
        List<PatientReimbursementRatioVO> ratioList = insuredPersonService.getReimbursementRatioByPatientId(patientId);
        return ResultVo.ok(ratioList);
    }
    


    @ApiOperation(value = "根据医院等级、人员类型和费用金额查询医保报销比例")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "hospitalLevel", value = "医院等级", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "personType", value = "人员类型", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "cost", value = "费用金额", required = true, dataType = "BigDecimal", paramType = "query")
    })
    @GetMapping("/reimbursement/ratio")
    public ResultVo<Hospital_reimbursement_ratioVO> getReimbursementRatioByParams(
            @RequestParam String hospitalLevel,
            @RequestParam String personType,
            @RequestParam BigDecimal cost) {
        Hospital_reimbursement_ratioVO ratioVO = insuredPersonService.getReimbursementRatio(hospitalLevel, personType, cost);
        return ResultVo.ok(ratioVO);
    }
}

